package com.yimi.yinhepda.views.createPackage_module;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;

import butterknife.Bind;

import com.yimi.yinhepda.R;
import com.yimi.yinhepda.adapter.CheckBillAdapter;
import com.yimi.yinhepda.entity.bean.CheckEntity;
import com.yimi.yinhepda.entity.response.BuildPackageDetailResponse;
import com.yimi.yinhepda.utils.IntentUtils;
import com.yimi.yinhepda.base.BaseInitActivity;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * @author 000432
 * @date 2017/4/11 0011
 * <p>
 * 查看运单信息(扫描页面点击一行可以)
 */
public class CreateWayBillDetailActivity extends BaseInitActivity implements View.OnTouchListener {
    /**
     * 标题栏左边
     */
    @Bind(R.id.title)
    TextView mTvTitle;
    /**
     * 标题栏中间
     */
    @Bind(R.id.num)
    TextView mTvNum;
    @Bind(R.id.activity_compare_waybill)
    EditText mEditWayBill;
    @Bind(R.id.activity_compare_listview)
    ListView mListView;
    @Bind(R.id.activity_compare_back)
    Button mBtnBack;
    /**
     * 总件数
     */
    @Bind(R.id.load_compare_zj)
    TextView mTvTotal;
    /**
     * 已扫描件数
     */
    @Bind(R.id.load_compare_scaned)
    TextView mTvScannedNum;
    /**
     * 未扫描件数
     */
    @Bind(R.id.load_compare_not_scaned)
    TextView mTvNotScannedNum;
    /**
     * 实重
     */
    @Bind(R.id.activity_compare_realWeightQty)
    TextView mTvRealWeight;
    /**
     * 计重
     */
    @Bind(R.id.activity_compare_meterageWeightQty)
    TextView mTvComputeWeight;
    /**
     * 体积
     */
    @Bind(R.id.activity_compare_volume)
    TextView mTvVolume;
    @Bind(R.id.dwbda_scrollView)
    ScrollView mScrollView;
    /**
     * 运单号
     */
    private String mCheckWayBillNo;
    private CheckBillAdapter mAdapter;
    private List<CheckEntity> mCheckList = new ArrayList<>();

    @Override
    public String getPageName() {
        return getString(R.string.page_create_billdetail);
    }

    @Override
    public int getContentViewId() {
        return R.layout.activity_compare;
    }

    @Override
    public void initView(Bundle savedInstanceState) {

        //标题
        mTvTitle.setText(getString(R.string.check_waybillno_title));

        //运单号
        mCheckWayBillNo = getIntent().getStringExtra(IntentUtils.BUILD_WAYBILLNO);
        mEditWayBill.setText(mCheckWayBillNo);
    }

    @Override
    public void initEvent() {
        mBtnBack.setOnClickListener(this);
        mListView.setOnTouchListener(this);
        mScrollView.setOnTouchListener(this);
    }

    /**
     * 由运单号，查询子单号，扫描状态等信息
     */
    @Override
    public void initData() {

        List<BuildPackageDetailResponse.StockInfo> buildList = CreatePackageScanActivity.buildList;

        for (int i = 0; i < buildList.size(); i++) {
            //当前行的运单信息，包括子单的集合
            BuildPackageDetailResponse.StockInfo currentItemInfo = buildList.get(i);
            String wayBillNo = currentItemInfo.getWaybillNo();

            if (wayBillNo != null && wayBillNo.equals(mCheckWayBillNo)) {
                List<BuildPackageDetailResponse.ChildInfo> childInfoList = currentItemInfo.getChildList();
                for (BuildPackageDetailResponse.ChildInfo childInfo : childInfoList) {
                    CheckEntity checkEntity = new CheckEntity();
                    checkEntity.setChildWayBillNo(childInfo.getChildWaybillNo());
                    if (childInfo.getFlag() == 0) {
                        checkEntity.setStatus(0);
                    } else {
                        checkEntity.setStatus(1);
                    }
                    //列表显示
                    mCheckList.add(checkEntity);

                    //总件数，已扫描，未扫描
                    mTvTotal.setText(childInfoList.size() + "");
                    mTvScannedNum.setText(currentItemInfo.getScannedNum() + "");
                    mTvNotScannedNum.setText((childInfoList.size() - currentItemInfo.getScannedNum()) + "");

                    //实重，计重，体积
                    mTvRealWeight.setText(currentItemInfo.getWeight() + "kg");
                    mTvComputeWeight.setText(currentItemInfo.getChargedWeight() + "kg");
                    mTvVolume.setText(currentItemInfo.getVolume() + "m³");
                }
                break;
            }
        }
        //comparator接口进行字符串的大小排序
        Collections.sort(mCheckList, new Comparator<CheckEntity>() {
            @Override
            public int compare(CheckEntity t0, CheckEntity t1) {
                return t0.getChildWayBillNo().compareTo(t1.getChildWayBillNo());
            }
        });
        Collections.sort(mCheckList);
        mAdapter = new CheckBillAdapter(this, mCheckList, null);
        mListView.setAdapter(mAdapter);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v.getId() == R.id.activity_compare_back) {
            activityBackMethod();
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
            activityBackMethod();
        }
        return false;
    }

    private void activityBackMethod() {
        finish();
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (v.getId()) {
            case R.id.activity_compare_listview:
                mListView.requestDisallowInterceptTouchEvent(true);
                break;
            default:
                mScrollView.requestDisallowInterceptTouchEvent(true);
                break;
        }
        return false;
    }
}
